package com.wang.vedio.player;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;
import com.wang.vedio.R;
import com.wang.vedio.adapter.index.SearchResultAdapter;
import com.wang.vedio.base.BaseActivity;
import com.wang.vedio.net.SharedHelper;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.entity.VideoBean;
import com.wang.vedio.net.rxjava.HttpMethods;
import com.wang.vedio.net.subscribers.ProgressSubscriber;
import com.wang.vedio.net.subscribers.SubscriberOnNextListener;
import com.wang.vedio.utils.statusbar.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LookVedioNoteActivity extends BaseActivity {
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rec_main)
    RecyclerView rec_main;
    @BindView(R.id.clearBtn)
    TextView clearBtn;


    private ArrayList<VideoBean> mList = new ArrayList<>();
    private SearchResultAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_look_vedio_note_layout;
    }

    private String key;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        setTitle("观看记录");
        clearBtn.setVisibility(View.VISIBLE);
        StatusBarUtil.setStatusBar(this, true, R.color.transparent, false, true);
        initSrl();
        init();
    }

    private void initSrl() {
        srlAll.setEnableRefresh(false);
        srlAll.setEnableLoadMore(true);
        srlAll.setEnableAutoLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            page = 1;
            mList.clear();
            getData();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            page++;
            getData();
        });
    }

    private void init() {
        mList.clear();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new SearchResultAdapter(R.layout.item_sousuo_video_result, mList);
        rec_main.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                getVedioById(mList.get(position).getId());
            }
        });

        mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                showTiShi(position);
                return false;
            }
        });
    }

    public void showTiShi(int position) {
        RxDialogSureCancel rxDialogEditSureCancel = new RxDialogSureCancel(mContext);
        rxDialogEditSureCancel.getLogoView().setVisibility(View.GONE);
        rxDialogEditSureCancel.getTitleView().setTextSize(18);
        rxDialogEditSureCancel.getTitleView().setTextColor(Color.BLACK);
        rxDialogEditSureCancel.setTitle("提示");
        rxDialogEditSureCancel.setContent("确定要删除该记录？");
        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
                //删除
                deleteNoteById(mList.get(position).getNoteId(),position);
            }
        });
        rxDialogEditSureCancel.show();
    }

    private void deleteNoteById(String noteId,int position){
        SubscriberOnNextListener<BaseBean<VideoBean>> onNextListener = baseBean -> {
            RxToast.normal("删除成功！");
            if("all".equals(noteId)){
                mList.clear();
            }else{
                mList.remove(position);
            }
            mAdapter.notifyDataSetChanged();
        };
        Map<String,Object> params = new HashMap<>();
        params.put("id",noteId);
        params.put("memberId", SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().deleteNoteById(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private void getVedioById(String vedioId){
        SubscriberOnNextListener<BaseBean<VideoBean>> onNextListener = baseBean -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("obj",baseBean.getData());
            RxActivityTool.skipActivity(mContext,PlayerActivity.class,bundle);
        };
        Map<String,Object> params = new HashMap<>();
        params.put("id",vedioId);
        params.put("memberId", SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().getVedioById(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }


    @Override
    protected void initData() {
        getData();
    }

    private int page = 1;
    private void getData() {
        SubscriberOnNextListener<BaseBean<List<VideoBean>>> onNextListener = baseBean -> {
            if (srlAll != null) {
                srlAll.finishLoadMore(true);
                srlAll.finishRefresh(true);
            }

            mList.addAll(baseBean.getData());
            mAdapter.notifyDataSetChanged();
        };
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", page + "");
        params.put("memberId", SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().getLookVedioNoteList(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    @OnClick({R.id.clearBtn})
    public void onClick(View v){
        if(v.getId() == R.id.clearBtn){
            deleteNoteById("all",-1);
        }
    }
}
